/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    gongwenkai
 * 创建时间:    2023-09-26
 */
package org.jeecg.modules.flow.callback.outbound.task;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.business.doc.outbound.service.IWmsOutReceiptMaterialService;
import org.jeecg.modules.business.doc.outbound.service.IWmsOutReceiptService;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTask;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskMaterialService;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskService;
import org.jeecg.modules.business.third.entity.EmsOrDmsDeliveryDto;
import org.jeecg.modules.business.third.service.IEmsOrDmsDeliveryOperateService;
import org.jeecg.modules.flow.callback.enums.FlowTypeEnum;
import org.jeecg.modules.flow.callback.outbound.util.CommonOutboundUtil;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * 横剪叫料-600通知叫料 回调Bean
 *
 * @author gongwenkai
 * @version 1.0.0
 * @since 2023-09-26
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class OutBoundTaskHJJLTL600NoticeAskBean<T> extends BaseCustomBeanRun<T> {

    private final IWmsOutReceiptTaskService wmsOutReceiptTaskService;

    private final IEmsOrDmsDeliveryOperateService emsOrDmsDeliveryOperateService;


    /**
     * 通知叫料
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void actureRun() {

        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        String taskId = baseFlowDto.getInfoId();
        baseFlowDto.translateClass(JSONObject.class);
        JSONObject jsonData = (JSONObject) baseFlowDto.getData();
        Boolean fromMDSFlag = jsonData.getBoolean("fromMDSFlag");
        if(Objects.isNull(fromMDSFlag)||fromMDSFlag==false){
            //是从上一个节点流转过来的
            //判断任务是否自动
            WmsOutReceiptTask outTask = wmsOutReceiptTaskService.getById(taskId);
            if(Objects.isNull(outTask)){
                throw new JeecgBootException("出库任务不存在");
            }
            //如果自动 主动调用MDS发送叫料请求
            if(FlowTypeEnum.AUTOMATIC.getCode().equals(outTask.getFlowType())){
                // 主动调用MDS发送叫料请求
                EmsOrDmsDeliveryDto emsOrDmsDeliveryDto=new EmsOrDmsDeliveryDto();
                emsOrDmsDeliveryDto.setTaskId(taskId);
                emsOrDmsDeliveryDto.setType(3);
                emsOrDmsDeliveryOperateService.outbound(emsOrDmsDeliveryDto);
                //并且卡住流程
                baseFlowDto.setRunThenStop(true);
            }
        }else{
            //从mds通知过来的，让流程放行
            baseFlowDto.setRunThenStop(false);
        }
        // 当前前端调用业务需要你返回的
        baseFlowDto.setResult(Result.OK());
        // id是保存业务记录的id。用于生成业务流程信息记录
        baseFlowDto.setInfoId(taskId);
    }
}
